package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.ruoyi.system.service.ISysPropertyCommentService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.domain.entity.SysComment;
import com.ruoyi.system.service.ISysCommentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 评论详情Controller
 * 
 * @author ssd
 * @date 2024-03-29
 */
@RestController
@Validated
@RequestMapping("/system/comment")
public class SysCommentController extends BaseController
{
    @Autowired
    private ISysCommentService sysCommentService;
    @Autowired
    private ISysPropertyCommentService sysPropertyCommentService;

    /**
     * 查询评论详情列表
     */
    @PreAuthorize("@ss.hasPermi('system:comment:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysComment sysComment)
    {
        startPage();
        List<SysComment> list = sysCommentService.selectSysCommentList(sysComment);
        return getDataTable(list);
    }

    /**
     * 导出评论详情列表
     */
    @PreAuthorize("@ss.hasPermi('system:comment:export')")
    @Log(title = "评论详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysComment sysComment)
    {
        List<SysComment> list = sysCommentService.selectSysCommentList(sysComment);
        ExcelUtil<SysComment> util = new ExcelUtil<SysComment>(SysComment.class);
        util.exportExcel(response, list, "评论详情数据");
    }

    /**
     * 获取评论详情详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:comment:query')")
    @GetMapping(value = "/{commentId}")
    public AjaxResult getInfo(@PathVariable("commentId") Long commentId)
    {
        return success(sysCommentService.selectSysCommentByCommentId(commentId));
    }

    /**
     * 新增评论详情
     */
    @PreAuthorize("@ss.hasPermi('system:comment:add')")
    @Log(title = "评论详情", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody SysComment sysComment)
    {
        sysComment.setCreateBy(getUsername());
        // 执行插入操作
        sysCommentService.insertSysComment(sysComment);
        // 获取新插入的 commentId
        Long commentId = sysComment.getCommentId();
        // 获取 ids
        List<Long> ids = sysComment.getIds();
        // 执行批量插入操作
        sysPropertyCommentService.insertSysPropertyCommentList(commentId, ids);
        return AjaxResult.success();
    }

    /**
     * 修改评论详情
     */
    @PreAuthorize("@ss.hasPermi('system:comment:edit')")
    @Log(title = "评论详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody SysComment sysComment)
    {
        sysComment.setUpdateBy(getUsername());
        return toAjax(sysCommentService.updateSysComment(sysComment));
    }

    /**
     * 删除评论详情
     */
    @PreAuthorize("@ss.hasPermi('system:comment:remove')")
    @Log(title = "评论详情", businessType = BusinessType.DELETE)
	@DeleteMapping("/{commentIds}")
    public AjaxResult remove(@PathVariable Long[] commentIds)
    {
        return toAjax(sysCommentService.deleteSysCommentByCommentIds(commentIds));
    }
}
